package com.squareshoot.picCheckin;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FirstStart extends Activity {
	
	private static final int GROUP1 = 0;
	private static final int ID_LINK = 1;
	private static final int DIALOG_QUIT = 10;
	EditText login,pwd;
	Button start;
	ProfileDb mDbHelper;
	
	private TextWatcher loginWatcher = new TextWatcher() {
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count,int after) {
			// TODO Auto-generated method stub
		}
		
		public void afterTextChanged(Editable s) {
			if(s.length()>0)
				enableButton();
			else
				enableButton();
		}
	};
	
	private TextWatcher pwdWatcher = new TextWatcher() {
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count,int after) {
			// TODO Auto-generated method stub
		}
		
		public void afterTextChanged(Editable s) {
			if(s.length()>0)
				enableButton();
			else
				enableButton();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(Common.HIGHDEBUG)
			Log.d(Common.TAG,"FirstStart : onCreate");
		setContentView(R.layout.firstlogin);
		login = (EditText)findViewById(R.id.login);
		pwd = (EditText)findViewById(R.id.password);
		login.addTextChangedListener(loginWatcher);
		pwd.addTextChangedListener(pwdWatcher);

		start = (Button)findViewById(R.id.firstStartButton);
		start.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
				SharedPreferences.Editor editor = prefs.edit();
				editor.putString("emailPref", login.getText().toString());
				editor.putString("passwordPref", pwd.getText().toString());
				editor.commit();

				mDbHelper = new ProfileDb(FirstStart.this);
				mDbHelper.open();
				mDbHelper.dropProfileTable();
				mDbHelper.createProfile("","","",null);
				mDbHelper.updateEmail(1,login.getText().toString());
				mDbHelper.updatePassword(1,pwd.getText().toString());
				mDbHelper.close();

				Intent i = new Intent();
				i.putExtra("login",login.getText().toString());
				i.putExtra("password",pwd.getText().toString());
				setResult(Activity.RESULT_OK,i);
				finish();

			}
		});

		Button loginWithFoursquare = (Button)findViewById(R.id.foursquareLoginButton);
		loginWithFoursquare.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				loginToForsquare();
			}
		});
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(GROUP1,ID_LINK,1, getString(R.string.signup)).setIcon(R.drawable.ic_menu_add);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ID_LINK:
			//	https://foursquare.com/mobile/signup
			String url = "https://foursquare.com/mobile/signup";
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(url));
			startActivity(i);
			return true;
		}
		return false;
	}

	@Override
	public boolean onKeyDown(int code,KeyEvent event){
		if(event.getKeyCode()==4){
			//touche BACK, on quitte
			showDialog(DIALOG_QUIT);
			return true;
		}
		return false;
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_QUIT:
			//Confirmation pour quitter l'appli (quand on appuie sur le bouton BACK)
			AlertDialog.Builder builderQuit = new AlertDialog.Builder(this);
			builderQuit.setTitle(getString(R.string.quit))
			.setCancelable(false)
			.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent mIntent = new Intent();
					mIntent.putExtra("quoi", "quit");
					setResult(RESULT_OK, mIntent);
					finish();
				}
			})
			.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alertQuit = builderQuit.create();
			return alertQuit;
		}
		return null;
	}

	private void enableButton(){
		if(login.length()>0 && pwd.length()>0)
			start.setEnabled(true);
		else
			start.setEnabled(false);
	}
	
	private void loginToForsquare() {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		String uriString = "https://foursquare.com/oauth2/authenticate" +
			"?client_id=" +Common.CLIENT_ID +
			"&response_type=code" +
			"&display=touch" +
			"&redirect_uri=sqshoot-android://"; 
		Log.d(Common.TAG,"URI: "+uriString);
		
		intent.setData(
				Uri.parse(uriString));
		startActivity(intent);
	}
}
